import matplotlib
import numpy as np
import matplotlib.pyplot as plt

matplotlib.rc("font", family='MicroSoft YaHei', weight="bold")


def get_losses(f):
    lines = f.readlines()
    tloss = []
    closs = []
    for l in lines:
        index_c = l.find("loss_cls")
        index_t = l.find("total_loss")
        if index_c != -1:
            c_str = l[index_c + 10:index_c + 15]
            closs.append(float(c_str))
        if index_t != -1:
            t_str = l[index_t + 12:index_t + 17]
            tloss.append(float(t_str))
    return tloss, closs


f1 = open("0620_44_bin_only.txt", "r")
_, c1 = get_losses(f1)

f2 = open("0622_65.txt", "r")
_, c2 = get_losses(f2)
c1 = c1[20:]
c2 = c2[9:]
num_losses = len(c1)


# c1 = c1[:num_losses]


def smooth_loss(c):
    inteval = 2
    c_l = []
    for i in range(0, num_losses, inteval):
        mean_value_c = np.mean(np.array(c[i:i + inteval]))
        c_l.append(mean_value_c)
    return c_l


new_c1 = smooth_loss(c1)
new_c2 = smooth_loss(c2)

x = list(range(len(new_c1)))
plt.plot(x, new_c1, label="bin_cls_loss_only")
plt.plot(x, new_c2, label="exp65")

plt.title("训练损失比较")
plt.legend()
plt.show()
